from typing import Optional
import flet as ft
import atexit
import pyuac
import win32gui
import os
import signal

from views.home_page import home_view
from views.settings_page import settings_view
from views.common import Page,init_page,mycmd,cleanup
from utils.config import config

def main(page: Page):
    def on_route_change(e: Page):
        page.views.clear()
        home_view(page)
        if page.route == "/settings":
            settings_view(page)
        page.update()

    init_page(page)
    page.theme = ft.Theme(
        color_scheme=ft.ColorScheme(
            primary="blue",
            # background="pink",
            # surface="yellow",
        )
    )
    page.title = "Flet_Test"
    page.vertical_alignment = "center"
    page.horizontal_alignment = "center"
    page.on_route_change = on_route_change
    page.window.icon = os.path.join(os.getcwd()+"/icon/","Genshin.ico")
    page.window.width = 650
    page.window.height = 650
    page.window.min_width = 650
    page.window.min_height = 650
    page.window.max_width = 650
    page.window.max_height = 650
    # page.window.always_on_top = True
    page.bgcolor = page.theme.color_scheme.background
    if config.dark_mode_Flag:
        page.theme_mode = ft.ThemeMode.DARK
    else:
        page.theme_mode = ft.ThemeMode.LIGHT

    page.update()

    page.go(page.route)

if __name__ == "__main__":
    # win32gui.ShowWindow(mycmd, 0)
    # atexit.register(cleanup)
    # if not pyuac.isUserAdmin():
    #     pyuac.runAsAdmin()
    # else:
        try:
            if config.debug_mode_Flag is False:
                win32gui.ShowWindow(mycmd, 0)
            else:
                win32gui.ShowWindow(mycmd, 1)
        except:
            pass
        ft.app(target=main)